<template>
<div class="rules">
  <el-card>
    <el-form
      :model="funds"
      size="small"
      :rules="rules"
      ref="fundsForm"
      label-width="80px"
      :hide-requied-aterisk="true"
    >
      <el-form-item
        class="query-form-item"
        prop="money"
        label="起购金额 : "
        :inline="true"
      >
        <el-input
          v-model="funds.money"
          placeholder="请输入金额"
          @keyup.enter.native="moneySubmit"
          clearable
          :maxlength="13"
          auto-complete="off"
          oninput="value=value.replace(/[^0-9.]/g,'')"
          style="width: 300px"
        >
          <i slot="prefix" style="font-style: normal; font-size: 16px">￥</i>
        </el-input>
        <el-button
          type="warning"
          style="margin-left: 20px"
          @click="moneySubmit('fundsForm')"
          >修改起购金额</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
  </div>
</template>
<script>
import {
  getStoreMoney,
  updateStoreMoney,
} from "../../api/myBusiness/setComRule";

export default {
  data() {
    let validateMoney = (rule, value, callback) => {
      console.log(rule);
      console.log(value);

      if (!value) {
        callback(new Error("金额不能为空或0"));
      } else if (value.split(".").length > 2) {
        callback(new Error("输入正确格式的金额")); //防止输入多个小数点
      } else {
        // value = Math.round(value * Math.pow(10,2)) / Math.pow(10,2) //四舍五入
        // value = Number(value).toFixed(2); //不足补位
        function getFloatNumber(num) {
          num += "";
          num = num.replace(/[^0-9|\.]/g, "");
          if (/^0+/) num = num.replace(/^0+/, "");
          if (!/\./.test(num)) num += ".00";
          if (/^\./.test(num)) num = "0" + num;
          num += "00";
          num = num.match(/\d+\.\d{2}/)[0];
          return num;
        }
        this.funds.money = getFloatNumber(value);
        callback();
      }
    };
    return {
      funds: { money: 0 },
      rules: {
        money: [{ validator: validateMoney, trigger: "blur" }],
      },
    };
  },
  created() {
    this.getStoreMoney();
  },
  methods: {
    getStoreMoney() {
      getStoreMoney()
        .then((data) => {
          if (data.code != 0) {
            this.$message.error(data.message);
            return false;
          }
          this.funds.money = data.data.buyfromquota;
          console.log(this.funds.money);
        })
        .catch((err) => {
          this.message.error("起购金额获取失败,请稍后重试");
        });
    },

    //提交金额
    moneySubmit(formName) {
      // if (this.funds.money == 0) {
      //   this.$message.error("调整金额不能为0");
      //   return false;
      // }

      const obj = {
        buyfromquota: this.funds.money,
      };
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm(`确认修改起购价格为${this.funds.money}吗?`, "提示", {
            type: "warning",
          }).then(() => {
            updateStoreMoney(obj)
              .then((data) => {
                if (data.code != 0) {
                  this.$message.error(data.message);
                  return false;
                }
                this.$message.success("修改成功");
                this.getStoreMoney();
              })
              .catch((err) => {
                this.$message.error("起购金额设置失败,请稍后重试!");
              });
          });
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
 
  .el-form{
    position: relative;
    left: 30%;
  }
  
</style>